package org.research;

/**
 * 位与算法：验证权限
 */
public class Limits {

	/**
	 * 验证权限
	 * 
	 * @param limitsSum
	 *            权限总和 为每个权限的3次方相加;
	 * @param checkInt
	 *            具体权限
	 * @return
	 */
	public static boolean checkLimits(String opelist, int checkInt) {
		int limitsSum = createLimits(opelist);
		return (limitsSum & (1 << checkInt)) > 0;
	}

	/**
	 * 生成权限的总值;
	 * 
	 * @param limits
	 * @return
	 */
	public static int createLimits(String opelist) {
		String limits[] = opelist.split(",");
		int limitsSum = 0;
		for (int i = 0; i < limits.length; i++) {
			limitsSum += (1 << Integer.parseInt(limits[i]));
		}
		return limitsSum;
	}
}
